/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package midpmpc;

import midpmpc.config.Server;
import org.j4me.logging.Log;
import org.j4me.ui.Dialog;
import org.j4me.ui.Menu;
import org.j4me.ui.MenuItem;
import org.j4me.ui.components.TextBox;

/**
 *
 * @author vvs
 */
public class ServerListView extends Menu implements CallbackMenuItem.MenuListener{
    Server tsrv;
    ServerEdit editForm;
    Mpc parent;
    Menu srvOpt;
    CallbackMenuItem addCmd,editCmd,delCmd;

    class SrvItem implements MenuItem
    {
        String n;
        public SrvItem(String name)
        {
            n=name;
        }
        public String getText()
        {
            return n;
        }
        public void onSelection()
        {

        }
    }
    class ServerEdit extends Dialog
    {
        TextBox addr,port,name;
        boolean isNew;
        ServerListView _parent;
        public ServerEdit(Server srv,boolean isnew,ServerListView parent)
        {
            setTitle("edit server");
            name=makeTextBox("Name", srv.name);
            addr=makeTextBox("Address", srv.addr);
            port=makeTextBox("Port", srv.port);
            append(name);
            append(addr);
            append(port);
            isNew=isnew;
            _parent=parent;
            setMenuText("Cancel", "Save");
        }
        TextBox makeTextBox(String title,String val)
        {
            TextBox ret=new TextBox();
            ret.setLabel(title);
            ret.setString(val);
            return ret;
        }
        public Server makeServer()
        {
            return new Server(name.getString(),addr.getString(),port.getString());
        }
        public void keyPressed(int key)
        {
            if (key==MENU_LEFT)
                _parent.show();
            else if (key==MENU_RIGHT)
            {
                //_parent.editSrv(isNew);
                Log.warn("saving srv");
                if (isNew)
                    _parent.parent.conf.addServer(makeServer(), _parent.getSelected());
                else
                    _parent.parent.conf.setServer(_parent.getSelected(),makeServer());
                _parent.parent.conf.saveSrvList();
                _parent.makeItems();
                _parent.show();
            }
            else
                super.keyPressed(key);
        }
    }
    public ServerListView(Mpc mid)
    {

        parent=mid;
        setMenuText("Menu", "Exit");
        makeItems();
        makeMenu();
    }
    void makeItems()
    {
        deleteAll();
        for (int i=0;i<parent.conf.srvCount();i++)
        {
            appendMenuOption(new SrvItem(parent.conf.getServer(i).name));
        }
    }
    void makeMenu()
    {
        srvOpt=new Menu("action", this);
        addCmd=new CallbackMenuItem("new", this);
        editCmd=new CallbackMenuItem("edit", this);
        delCmd=new CallbackMenuItem("del", this);
        srvOpt.appendMenuOption(addCmd);
        srvOpt.appendMenuOption(editCmd);
        srvOpt.appendMenuOption(delCmd);
    }
    public void keyPressed(int key)
    {
        if (key==MENU_LEFT)
            srvOpt.show();
        else if (key==MENU_RIGHT)
            parent.destroyApp(true);
        else if (key==FIRE)
        {
            if (size()==0)
            {
                Log.error("null srv");
                return;
            }
            parent.connect(selectedSrv());
        }
        else
            super.keyPressed(key);
    }
    public void editSrv(boolean isNew)
    {
        if (isNew)
            tsrv=new Server("New Server", "127.0.0.1", "6600");
        else
            tsrv=parent.conf.getServer(getSelected());
        editForm=new ServerEdit(tsrv,isNew,this);
        editForm.show();
    }
    public String selectedSrv()
    {
        if(getSelected()>=parent.conf.srvCount())
        {
            return null;
        }
        return parent.conf.getServer(getSelected()).toAddr();

    }
    public void onMenuSelection (CallbackMenuItem item)
    {
        Log.warn("ServerList: menu: "+item.getText());
        if (item==editCmd)
        {

            editForm=new ServerEdit(parent.conf.getServer(getSelected()), false, this);
            editForm.show();
        }
        else if (item==addCmd)
        {
            editForm=new ServerEdit(new Server("New", "192.168.2.1", "6600"), true, this);
            editForm.show();

        }
        else if (item==delCmd)
        {
            parent.conf.delServer(getSelected());
            parent.conf.saveSrvList();
            makeItems();
            show();
        }
    }


}
